var car_brand_id = 0; //品牌id
var car_series_id = 0; //车系id
var imgType_id = 0; //大图类型id
var Model_id = 0; //车型id
var Color_id = 0; //颜色id
var IndexS = 0; //车型首页下拉框选中下标

$(function() {
	get_user_mess_and_token();
	var url = location.search;
	url_index = url.indexOf('/');
	car_brand_id = url.substring(url.indexOf('?') + 1, (url_index != -1 ? url_index : url.length));
	car_series_id = url.split("/")[1];
	Model_id = url.split("/")[2];
	imgType_id = url.split("/")[3];
	Color_id = url.split("/")[4];
	IndexS = url.split("/")[5];
	//下拉框
	getXiaLaKuang();
	//当前位置
	getthisLocation();
	//外观内饰等等
	getImages_Type();
	//查询所有颜色
	getColor(imgType_id, Model_id);
	//查询车型名称以及最低价等等
	getCar_Model_Img(imgType_id, Model_id, Color_id);
	//回到顶部
	getGotoTop();
	//在售加载事件
	ZaiShoLoad();
	// 页面文字“不限车型”的加载事件
	NoModelLoad();
	//页面文字“不限颜色”的加载事件
	NoColorLoad();
	//下拉框移入移出事件
	RuLoad();
	//登录
	$(".to_login").attr("href",
		"http://127.0.0.1:8899/html/car_login.html?car_actual_large_picture/" + car_brand_id + "/" +
		car_series_id + "/" + Model_id + "/" + imgType_id + "/" + Color_id + "/" + IndexS);
});

/**
 * 下拉框移入移出加载事件
 */
function RuLoad() {
	$("#xuan").mouseover(function() {
		$(".xialaXuan").show();
	}).mouseout(function() {
		$(".xialaXuan").hide();
	});
}

/**
 * 在售加载事件
 */
function ZaiShoLoad() {
	//页面文字“在售”的点击事件
	$("#ZaiSho").click(function() {
		//给下拉框下面span移除样式
		$(".xialaXuan span").removeClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		$(".xialaXuan span").addClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq"
		);
		//给下拉框span选中（追加样式）
		$(".xialaXuan span:eq(0)").addClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		$(this).addClass("tw-relative filters_model-trigger__kmTof filters_selected__3gPXF");
		$("#NoModels").removeClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		$("#NoModels").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq");
		$(".xialaXuan").show();
		getCar_Model_Img(imgType_id, 0, Color_id);
	});
}

/**
 * 页面文字“不限车型”的加载事件
 */
function NoModelLoad() {
	//页面文字“不限车型”的点击事件
	$("#NoModels").click(function() {
		//给下拉框span取消选中（移除样式）
		$(".xialaXuan span").removeClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		//给下拉框span添加普通样式
		$(".xialaXuan span").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq ");
		$(this).addClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		$("#ZaiSho").removeClass("tw-relative filters_model-trigger__kmTof filters_selected__3gPXF");
		$("#ZaiSho").addClass("tw-relative filters_model-trigger__kmTof");
		$(".xialaXuan").hide();
		getColor(imgType_id, 0);
		getCar_Model_Img(imgType_id, 0, Color_id);
	});
}

/**
 * 页面文字“不限颜色”的加载事件
 */
function NoColorLoad() {
	//页面文字“不限颜色”的点击事件
	$("#NoColors").click(function() {
		$(this).addClass(
			"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
		);
		$("#Specific_color li").removeClass("filters_color-item__3ZHsM filters_selected__3gPXF");
		$("#Specific_color li").addClass("filters_color-item__3ZHsM");
		getCar_Model_Img(imgType_id, Model_id, 0);
	});
}

/**
 * 当前位置
 */
function getthisLocation() {
	$.post("http://127.0.0.1:8899/api/consumer/Car_ModelController/selectBy_brand_series_model_Id/" +
		car_brand_id + "/" + car_series_id + "/0",
		function(data) {
			str = "<li class='breadcrumb_link-item__3oAOk '>" +
				"<a target='_blank' href='http://127.0.0.1:8899/html/car_vehicle_selection.html' rel='opener'>" +
				"<span class='breadcrumb_fragment-text__yFR68'>选车</span></a>" +
				"<i class='DCD_Icon icon_into_12 breadcrumb_divider-icon__-YvK3'></i>" +
				"</li>" +
				"<li class='breadcrumb_link-item__3oAOk '>" +
				"<a target='_blank' href='http://127.0.0.1:8899/html/car_vehicle_selection.html?" + data.brand
				.brand_id + ","+data.brand.brand_name+"-x-x-x-x-x-x' rel='opener'>" +
				"<span class='breadcrumb_fragment-text__yFR68'>" + data.brand.brand_name + "</span></a>" +
				"<i class='DCD_Icon icon_into_12 breadcrumb_divider-icon__-YvK3'></i>" +
				"</li>" +
				"<li class='breadcrumb_link-item__3oAOk '>" +
				"<a target='_blank' href='http://127.0.0.1:8899/html/car_series_index.html?" + data.car_series_id +
				"' rel='opener'>" +
				"<span class='breadcrumb_fragment-text__yFR68'>" + data.car_series_name + "</span></a>" +
				"<i class='DCD_Icon icon_into_12 breadcrumb_divider-icon__-YvK3'></i></li>" +
				"<li class='breadcrumb_link-item__3oAOk breadcrumb_latest__1TBiB'>" +
				"<span class='breadcrumb_fragment-text__yFR68'>实拍大图</span>" +
				"</li>";
			$("#thisLocation li").remove();
			$("#thisLocation").append(str);
		}, "JSON");
}

/**
 * 外观内饰等等类型名称
 */
function getImages_Type() {
	$.post("http://127.0.0.1:8899/api/consumer/car_model_ImagesController/select_car_model_imgType", function(data) {
		var str = "";
		$(data).each(function(index, item) {
			if (index == 1) {
				str += "<li class='tabs_item__9Yc57'  onclick=\"getImage_TypeClick(this," + item
					.car_model_images_type_id + ",0)\"><a class='tabs_link__Up2RP'  rel='opener'>" +
					item
					.car_model_images_type_name +
					"<img src='//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/360-b27b2eac9f456748d80e..png' width='30'></a></li>";
			} else {
				str += "<li class='tabs_item__9Yc57'  onclick=\"getImage_TypeClick(this," + item
					.car_model_images_type_id + ",0)\"><a class='tabs_link__Up2RP'  rel='opener'>" +
					item
					.car_model_images_type_name + "</a></li>";
			}
			//如果是大图类型id4（车展）就隐藏颜色
			if (imgType_id == 4) {
				$("#colors").hide();
			} else {
				$("#colors").show();
			}
		})
		$("#Image_Type li").remove();
		$("#Image_Type").append(str);
		$("#Image_Type li:eq(0)").addClass("tabs_item__9Yc57 tabs_active__2VN89");
		//加载根据传入大图类型id更改大图文字样式选中
		ImgTypeLoad();
	}, "JSON");
}

/**
 * 根据传入大图类型id更改大图文字样式的加载事件
 */
function ImgTypeLoad() {
	var indexs = imgType_id - 1;
	if (imgType_id == 1) {
		getLoadChange(indexs);
	} else if (imgType_id == 2) {
		getLoadChange(indexs);
	} else if (imgType_id == 3) {
		getLoadChange(indexs);
	} else if (imgType_id == 4) {
		getLoadChange(indexs);
	}
}

/**
 * 样式加载时改变
 */
function getLoadChange(indexs) {
	$("#Image_Type li").removeClass("tabs_item__9Yc57 tabs_active__2VN89"); //移除除当前对象的其他兄弟对象的加粗样式
	$("#Image_Type li:eq(" + indexs + ")").addClass("tabs_item__9Yc57 tabs_active__2VN89"); //追加字体加粗样式
	$("#Image_Type li").addClass("tabs_item__9Yc57"); //给其他兄弟对象追加普通样式
}

/**
 * 大图类型文字点击事件
 */
var type_id = 1;

function getImage_TypeClick(element, car_model_images_type_id, car_model_id) {
	type_id = car_model_images_type_id;
	//点击装饰，空间。。。给下拉框移除选中样式
	$(".xialaXuan span").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V");
	//点击装饰，空间。。。给下拉框追加普通样式
	$(".xialaXuan span").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq");
	$(element).addClass("tabs_item__9Yc57 tabs_active__2VN89"); //追加字体加粗样式
	$(element).siblings("li").removeClass("tabs_item__9Yc57 tabs_active__2VN89"); //移除除当前对象的其他兄弟对象的加粗样式
	$(element).siblings("li").addClass("tabs_item__9Yc57"); //给其他兄弟对象追加普通样式
	//给不限颜色加上选中样式
	$("#NoColors").addClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
	);
	//加载颜色方法
	getColor(car_model_images_type_id, car_model_id);
	getCar_Model_Img(car_model_images_type_id, car_model_id, 0);
	if (car_model_images_type_id == 4) {
		$("#colors").hide();
	} else {
		$("#colors").show();
	}
}

/**
 * 查询下拉框内容
 */
function getXiaLaKuang() {
	$.post("http://127.0.0.1:8899/api/consumer/Car_ModelController/modelList/" + car_series_id, function(data) {
		var str = "";
		$(data).each(function(index, item) {
			if (index == 0) {
				str +=
					"&nbsp;&nbsp;&nbsp;&nbsp;<span class='filters_checkbox__1KXsV dcar-radio radio_radio__14TOq ' onclick=\"getXialaClick(this,0)\"><a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;全部</a></span><br/><br/>";
			}
			str +=
				"&nbsp;&nbsp;&nbsp;&nbsp;<span class='filters_checkbox__1KXsV dcar-radio radio_radio__14TOq ' onclick=\"getXialaClick(this," +
				item.car_model_id + ")\"><a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + item.car_model_name +
				" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong style='color: red;'>" +
				item.car_model_manufacturer_price + "万</strong></a></span><br/><br/>";
		})
		$(".xialaXuan span").remove();
		$(".xialaXuan").append(str);
		//下拉框加载改变的加载事件
		if (Model_id != 0) {
			getXiaLaLoad();
		}
	}, "JSON");
}

/**
 * 下拉框加载改变的加载事件
 */
function getXiaLaLoad() {
	//给下拉框下面span移除勾选样式
	$(".xialaXuan span").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
	);
	//给下拉框下面span添加普通样式
	$(".xialaXuan span").addClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq"
	);
	//给下拉框span选中（追加样式）勾选样式
	$(".xialaXuan span:eq(" + IndexS + ")").addClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
	);
	//点击在售追加一个黄色样式
	$("#ZaiSho").addClass("tw-relative filters_model-trigger__kmTof filters_selected__3gPXF");
	//移除不限车型的选中样式
	$("#NoModels").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V"
	);
	//给不限车型添加一个普通样式
	$("#NoModels").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq");
}

/**
 * 下拉框点击事件
 */
function getXialaClick(element, model_id) {
	if (model_id == '' || model_id == null) {
		Model_id = 0;
	} else {
		Model_id = model_id;
	}
	$("#NoModels").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V");
	$("#NoModels").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq");
	$("#ZaiSho").addClass("tw-relative filters_model-trigger__kmTof filters_selected__3gPXF");
	$(element).addClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V");
	$(element).siblings("span").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V");
	$(element).siblings("span").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq");
	//加载颜色方法
	getColor(type_id, Model_id); //----------------下拉框车型id加车系大图类型id查询颜色
	getCar_Model_Img(type_id, Model_id, 0);
	$(".xialaXuan").hide();
}

/**
 * 查询所有颜色
 */
function getColor(car_model_images_type_id, car_model_id) {
	if (car_model_images_type_id == '' || car_model_images_type_id == null) {
		imgType_id = 0;
	} else {
		imgType_id = car_model_images_type_id;
	}

	if (car_model_id == '' || car_model_id == null) {
		Model_id = 0;
	} else {
		Model_id = car_model_id;
	}
	$.post("http://127.0.0.1:8899/api/consumer/car_model_ImagesController/select_specific_color/" + imgType_id + "/" +
		Model_id,
		function(data) {
			var str = "";
			$(data).each(function(index, item) {
				str += "<li class='filters_color-item__3ZHsM' onclick=\"getColorClick(this," + imgType_id +
					"," + Model_id + "," + item.specific_color.specific_color_id + ")\">" +
					"<span class='filters_color-gallery__TBq5r'>" +
					"<span class='filters_color__3-TeN' style='background-color:" + item.specific_color
					.specific_color_value + "'></span>" +
					"</span>" +
					"<span class='tw-relative filters_name__3PGvb'>" + item.specific_color
					.specific_color_name + "</span></li>";
			})
			$("#Specific_color li").remove();
			$("#Specific_color").append(str);
		}, "JSON");
}

/**
 * 点击颜色事件 
 */
function getColorClick(element, car_model_images_type_id, car_model_id, specific_color_id) {
	imgType_id = car_model_images_type_id;
	Model_id = car_model_id;
	Color_id = specific_color_id;
	$(element).addClass("filters_color-item__3ZHsM filters_selected__3gPXF");
	$(element).siblings("li").removeClass("filters_color-item__3ZHsM filters_selected__3gPXF");
	$(element).siblings("li").addClass("filters_color-item__3ZHsM");
	$("#NoColors").removeClass(
		"filters_checkbox__1KXsV dcar-radio radio_radio__14TOq radio_checked__67V3t radio_has-children__1tF1V");
	$("#NoColors").addClass("filters_checkbox__1KXsV dcar-radio radio_radio__14TOq ");
	getCar_Model_Img(imgType_id, Model_id, Color_id);
}


/**
 * 查询车型名称以及最低价,图片
 */
function getCar_Model_Img(car_model_images_type_id, car_model_id, specific_color_id) {
	if (car_model_images_type_id == '' || car_model_images_type_id == null) {
		imgType_id = 1;
	} else {
		imgType_id = car_model_images_type_id;
	}
	if (car_model_id == '' || car_model_id == null) {
		Model_id = 0;
	} else {
		Model_id = car_model_id;
	}
	if (specific_color_id == '' || specific_color_id == null) {
		Color_id = 0;
	} else {
		Color_id = specific_color_id;
	}
	//alert("获取到的id分别为："+imgType_id+"--"+car_model_id+"--"+Color_id);
	$.post("http://127.0.0.1:8899/api/consumer/car_model_ImagesController/select_car_model_imgById/" + car_brand_id +
		"/" + car_series_id + "/" + Model_id + "/" + imgType_id + "/" + Color_id,
		function(data) {
			var str = "";
			$(data).each(function(index, item) {
				str += "<section class='list_image-section__iqEH4'>" +
					"<div class='car_root__W_Dnr'>" +
					"<h2 class='car_car-name__2tffi'>" +
					"<a href='http://127.0.0.1:8899/html/car_model_index.html?" + car_brand_id + "/" +
					car_series_id + "/" + item.car_model_id +
					"' target='_blank' rel='opener'>" + item
					.car_model_name + "<i class='DCD_Icon icon_triangle_right'></i></a>" +
					"</h2>" +
					"<span style='display:inline-block;width:1px;height:16px;background:#C9CBD6'></span>" +
					"<p class='car_car-price__3fM3O'>最低售价：<span class='car_price__2RNZg'>" + item.car_series
					.min_distributer_price + "万起</span></p>" +
					"<p class='car_car-reduce__131BR'>最高降价：<span class='car_price__2RNZg'><i class='DCD_Icon icon_reduce'></i>" +
					item.car_model_max_decline + "万</span></p>" +
					"<a class='car_car-dealer__ZeW5Y common-link_root__2VBT4' target='_blank' href='http://127.0.0.1:8899/html/car_purchase_calculation.html?" +
					item.car_series.car_series_id + "/" +item.car_model_id+
					"'>购车计算</a>" +
					"</div><ul class=\"list_image-list__1hGFr\">";
				$(item.car_series.brand.car_model_imagesList).each(function(indexs, items) {
					if (indexs < 15) {
						str += "<li class='list_image-item__1Uw4s'>" +
							"<a href='javascript:void(0)' target='_blank' rel='opener'>" +
							"<div style='display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0' class='list_image__38_y4'>" +
							"<div style='box-sizing:border-box;display:block;max-width:100%'>" +
							"<img style='max-width:100%;display:block;margin:0;border:none;padding:0' alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;256&quot; height=&quot;170&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>'aria-hidden='true' role='presentation'>" +
							"</div>" +
							"<img src='http://120.48.47.144:8899/images/" + items
							.car_model_images_url +
							"' width='256' height='170' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class=''>" +
							"</div>" +
							"</a>"
						if (indexs == 14) {
							str +=
								"<span class='list_view-more__3khjM' onclick=\"getClickLookMoreImg(this)\"><span class='list_view-more-text__1jTKj'>查看更多<i class='DCD_Icon icon_triangle_right'></i></span></span>";
						}
						"</li>";
					}
					if (indexs >= 15) {
						str += "<li class='list_image-item__1Uw4s' style='display:none'>" +
							"<a href='javascript:void(0)' target='_blank' rel='opener'>" +
							"<div style='display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0' class='list_image__38_y4'>" +
							"<div style='box-sizing:border-box;display:block;max-width:100%'>" +
							"<img style='max-width:100%;display:block;margin:0;border:none;padding:0' alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;256&quot; height=&quot;170&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>'aria-hidden='true' role='presentation'>" +
							"</div>" +
							"<img src='http://120.48.47.144:8899/images/" + items
							.car_model_images_url +
							"' width='256' height='170' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class=''>" +
							"</div>" +
							"</a>"
						"</li>";
					}
				});
				str += "</ul></section>";
			})
			$("#Model_Img_section section").remove();
			$("#Model_Img_section").append(str);
		}, "JSON");
}

/**
 * 点击查看更多图片
 */
function getClickLookMoreImg(element) {
	//显示当前节点下面的图片
	$(element).parent().siblings("li").show();
	//移除当前节点样式
	$(element).remove();
}

/**
 * 火箭升空式回到顶部
 */

function getGotoTop(windowTop) {
	//向上移动到0px
	$("#gotoTop").click(function() {
		$('html,body').animate({
			scrollTop: '0px'
		}, 'slow');
	});

	//获取最小高度，无法入值则默认10
	windowTop ? windowTop = windowTop : windowTop = 10;

	//为窗口的scroll事件绑定函数
	$(window).scroll(function() {
		//获取当前高度
		var ss = $(window).scrollTop();
		if (ss > windowTop) {
			$("#gotoTop").fadeIn(500); //500毫秒速度
		} else {
			$("#gotoTop").fadeOut(500);
		}
	})
}
